<?php

namespace App\Http\Controllers\Admin;

use App\Models\ExternalDataRecord;
use App\Models\ResourceTotalType;
use App\Validate\ExternalDataRecordValidate;


/**
 * 对外数据记录管理
 */
class ExternalDataRecordController extends CommonController
{
    public $model = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->validate = new ExternalDataRecordValidate();
        $this->model = new ExternalDataRecord();
    }



    /**
     * 获取对外数据列表
     * @param keywords 检索条件
     * @param start_time int 开始时间
     * @param end_time int 结束时间
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function lists()
    {
        $limit = $this->request->input('limit', 10);
        $page = $this->request->input('page', 1);
        $start_time = $this->request->start_time;
        $end_time = $this->request->end_time;
        $res = $this->model->lists($this->request->keywords, $start_time, $end_time, $limit);
        if (empty($res['data'])) {
            return $this->returnApi(203, '暂无数据');
        }
        //增加序号
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);
        $res = $this->disPageData($res);
        return $this->returnApi(200, '获取成功', true, $res);
    }

    /**
     * 详情
     * @param id int 数据id
     */
    public function detail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->detail($this->request->id);

        if (!$res) {
            return $this->returnApi(201, "参数传递错误");
        }
        $res = $res->toArray();
        $res['content'] = json_decode($res['content'], true);

        return $this->returnApi(200, "获取成功", true, $res);
    }

    /**
     * 新增
     * @param title string 名称
     * @param content string 内容    json格式数据  值为 value1  value2 有多少列就有多少个value ，有多少行数据，就有多少个 value1 value2
     * @param intro string 简介
     */
    public function add()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $is_exists = $this->model->nameIsExists($this->request->title, 'title');

        if ($is_exists) {
            return $this->returnApi(202, "该名称已存在");
        }
        $res = $this->model->add($this->request->all());
        if (!$res) {
            return $this->returnApi(202, "保存失败");
        }
        return $this->returnApi(200, "保存成功", true);
    }

    /**
     * 修改
     * @param id int 数据id
     * @param title string 名称
     * @param content string 内容     json格式数据  值为 value1  value2 有多少列就有多少个value ，有多少行数据，就有多少个 value1 value2
     * @param intro string 简介
     */
    public function change()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $is_exists = $this->model->nameIsExists($this->request->title, 'title', $this->request->id);

        if ($is_exists) {
            return $this->returnApi(202, "该名称已存在");
        }

        $res = $this->model->change($this->request->all());
        if (!$res) {
            return $this->returnApi(202, "修改失败");
        }
        return $this->returnApi(200, "修改成功", true);
    }

    /**
     * 删除
     * @param id int 数据id
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->del($this->request->id);
        if ($res === true) {
            return $this->returnApi(200, "删除成功", true);
        }
        return $this->returnApi(202, $res);
    }
}
